package com.hospital.insurancedip.handler;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.SystemClock;
import com.hospital.common.core.exception.ExceptionPrint;
import com.hospital.common.core.function.PushMessage;
import com.hospital.common.core.result.ErrorTip;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.DateUtil;
import com.hospital.common.core.utils.SpringContextHolder;
import com.hospital.insurancedip.async.WxPayFunc;
import com.hospital.insurancedip.module.mapper.InsuranceLogsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: Yeman
 * @Date: 2023-04-03-14:54
 * @Description:
 */
@Slf4j
abstract class AbstractMessageInfo<T extends Tip> implements MessageInfo {
    private RestTemplate restTemplate = SpringContextHolder.getBean(RestTemplate.class);
    private PushMessage pushMessage = SpringContextHolder.getBean(PushMessage.class);
    private WxPayFunc wxPayFunc = SpringContextHolder.getBean(WxPayFunc.class);
    private InsuranceLogsMapper insuranceLogsMapper = SpringContextHolder.getBean(InsuranceLogsMapper.class);

    @Override
    public Tip getHisInfo(Tip requestVal) {
        Tip tip = null;
        String endesUrl = insuranceLogsMapper.getDipUrl();
        if (ObjectUtil.isNull(endesUrl)) {
            tip = new ErrorTip(1001, "银海明细费用上传接口地址’DIP_API_URL‘未配置！");
            return tip;
        }
        String startTime = DateUtil.getAllTime();
        StopWatch stopwatch2 = new StopWatch();
        stopwatch2.start();
        String infno = "";
        try {
            Object data = requestVal.getData();
            JSONObject jsonObject = JSON.parseObject(data.toString());
            infno = jsonObject.getString("infno");
            tip = getUpDetail(jsonObject);
            if (!(infno.equals("990206")&&(jsonObject.getString("fixmedins_code").equals("H61030200230") || jsonObject.getString("fixmedins_code").equals("H61032302183")))&& ObjectUtil.isNotNull(tip.getData())) {
                tip = pushMessage.sendRefundBillMsg(tip.getData().toString(), endesUrl, restTemplate);
            }
        } catch (Exception e) {
            tip = new ErrorTip(203, e.getMessage());
        } finally {
            stopwatch2.stop();
            String endTime = DateUtil.getAllTime();
            long end = SystemClock.now();
            ExceptionPrint.printCommErrorString("银海DIP统一接口-事件：" + infno,
                    requestVal.getData().toString(),
                    String.valueOf(tip.getCode())
                    , tip.getMessage() + "--返回内容：" + tip.getData(), startTime, endTime, String.valueOf(stopwatch2.getTotalTimeSeconds()));
        }
        return tip;
    }

    @Override
    public Tip getPlatformInfo(Tip requestVal, String platfromUrl) {
        Tip tip = new SuccessTip(null);
        String infno = "";
        try {
            Object data = requestVal.getData();
            JSONObject jsonObject = JSON.parseObject(data.toString());
            infno = jsonObject.getString("infno");
            wxPayFunc.platformPush(requestVal.getData().toString(), platfromUrl, infno);
        } catch (Exception e) {
            tip = new ErrorTip(203, e.getMessage());
        }
        return tip;
    }

    /**
     * @param requestVal:
     * @description:组装不同的input内容
     * @author: lvyao
     * @date: 2023/4/3 15:15
     * @return: T
     **/
    abstract T getUpDetail(JSONObject requestVal);

    /**
     * @description:处理完成后是否需要处理格式或数据组装再转发
     * @author: lvyao
     * @date: 2023/5/19 11:39
     * @param requestVal:
     * @return: T
     **/
    abstract T sendOther(JSONObject requestVal);
}
